package jespeak.rules;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LLGroup implements Group {

	private char[] name;
	private List rules;

	public LLGroup(char l1, char l2) {
		name = new char[2];
		name[0] = l1;
		name[1] = l2;
		rules = new ArrayList<Rule>();
	}

	@Override
	public String toString() {
		String s = "" + name[0] + name[1];
		return s;
	}

	public boolean addRule(Rule r) {
		return rules.add(r);
	}

	public MatchedRule match(String s, int position) {
		Collections.sort(rules);
		int nrules = rules.size();
		int max = 0, i;
		MatchedRule winner = null;
		for (i = 0; i < nrules; ++i) {
			Rule r = (Rule) rules.get(i);
			MatchedRule mr = r.match(s, position);
			if (mr == null) continue;
			int score = mr.getScore();
			if (score > max) {
				max = score;
				winner = mr;
				System.out.println("\tLL match: " + score + " " +
						  mr.getRule());
			}
		}
		return winner;
	}
	
}
